package org.delphy.indy2.views
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	import mx.containers.Canvas;
	import mx.controls.Image;

	public class VPresentationPanel extends Canvas
	{
		private var vBar:Image;
		
		[Embed(source='assets/vBar.png')]
		private var vBarImg:Class;
		
		private var firstRun:Boolean=true;
		
		public var selectedIndex:Number;
		
		public var transitionTime:Number=0.2;
		
		private var shadeSprite:Sprite;
		private var clippingMask:Sprite;
		
		public function VPresentationPanel()
		{
			super();
			
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
			vBar=new Image;
			vBar.source=vBarImg;
			vBar.width=120;
			vBar.height=600;
			
			rawChildren.addChild(vBar);
			
			shadeSprite=new Sprite();
			rawChildren.addChild(shadeSprite);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			vBar.x=width-vBar.width;
			vBar.y=(height-vBar.height)/2;	
			
			if(firstRun){
				arrangeChildren();
				firstRun=false;
			}
			
			applyShade();
			applyClippingMask();
		}
		
		private function arrangeChildren():void{
			var heightSum:Number=0
			
			for(var i:uint=0;i<numChildren;i++){
				var child:DisplayObject=getChildAt(i);
				
				child.width=width-30;
				child.x=0;
				child.y=height/2-heightSum-child.height/2;
				
				heightSum+=child.height;
			}		
			
			selectedIndex=0;
			
		}
		
		public function shiftDown():void{
			var child:DisplayObject;

			if(selectedIndex==numChildren-1){
				
				var heightSum:Number=0;
				
				for(var j:uint=0;j<numChildren;j++){
					child=getChildAt(j);
					heightSum+=child.height;
				}
				
				for(var idx:uint=0;idx<numChildren;idx++){
					child=getChildAt(idx);
					child.y-=heightSum;
				}				
				
			}
			
			for(var i:uint=0;i<numChildren;i++){
				child=getChildAt(i);
				Tweener.addTween(child,{y:child.y+child.height,time:transitionTime});	
			}
			
			if(selectedIndex==numChildren-1)
				selectedIndex=0;
			else
				selectedIndex++;
			
		}
		
		public function shiftUp():void{
			var child:DisplayObject;

			if(selectedIndex==0){
				
				var heightSum:Number=0;
				
				for(var j:uint=0;j<numChildren;j++){
					child=getChildAt(j);
					heightSum+=child.height;
				}
				
				for(var idx:uint=0;idx<numChildren;idx++){
					child=getChildAt(idx);
					child.y+=heightSum;
				}				
				
			}
			
			for(var i:uint=0;i<numChildren;i++){
				child=getChildAt(i);
				Tweener.addTween(child,{y:child.y-child.height,time:transitionTime});	
			}
			
			if(selectedIndex==0)
				selectedIndex=numChildren-1;
			else
				selectedIndex--;
			
		}
		
		private function applyShade():void{
			var g:Graphics=shadeSprite.graphics;
			g.clear();
			
			var mat:Matrix=new Matrix();
			mat.createGradientBox(width,height,Math.PI/2);

			g.beginGradientFill(GradientType.LINEAR,[0,0,0],[1,0,1],[50,128,205],mat);
			g.drawRect(0,0,width,height);
			g.endFill();
		}
		
		private function applyClippingMask():void{
			var gap:Number=40;
			
			var clippingMask:Sprite=new Sprite();
			clippingMask.graphics.clear();
			
			clippingMask.graphics.beginFill(0x000000);
			clippingMask.graphics.drawRect(x,y+gap, width, height-gap*2);
			
			this.mask = clippingMask;
		}
		
	}
}